package com.hqyj.zss.controller;

import com.hqyj.zss.pojo.Customer;
import com.hqyj.zss.pojo.Menus;
import com.hqyj.zss.pojo.PageData;
import com.hqyj.zss.service.CustomerService;
import com.hqyj.zss.service.MenusService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private MenusService menusService;

    /**
     *  用户获取个人信息
     * @param username
     * @param modelMap
     * @return
     */
    @RequestMapping("/getInfo")
    public String getInformation(String username, ModelMap modelMap){
        Customer oneByUserName = customerService.getOneByUserName(username);
        modelMap.put("customer",oneByUserName);
        System.out.println(oneByUserName);
        return "customer_home";
    }

    /**
     * 修改个人信息
     * @param customer
     * @param modelMap
     * @return
     */
    @RequestMapping("/modifyInfo")
    public String modifyInformation(Customer customer,ModelMap modelMap){
        int i = customerService.modifyCustomerInfo(customer);
        Customer newCustomer = customerService.getOneById(customer.getC_id());
        System.out.println(newCustomer);
        modelMap.put("customer",newCustomer);
        System.out.println(i);
        return "customer_home";
    }
    /**
     * 登录控制
     * @param username
     * @param password
     * @return 返回请求参数，或者用户信息
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> login(String username, String password){
        //检查请求参数
        if (username == null || password == null){
            Map<String,Object> result = new HashMap<>();
            result.put("code",-9);
            result.put("message","请求参数错误");
            return  result;
        }
        //调用服务层
        return customerService.customerLogin(username,password);
    }

    /**
     * 登出控制
     * @return
     */
    @RequestMapping("/logout")

    public String logout(){
        customerService.customerLogout();

        return "redirect: /OrderSystem_war_exploded/index.jsp";
    }

    /**
     * 跳转到注册界面
     * @return
     */
    @RequestMapping("/registerPage")
    public String registerPage(){
        return "redirect: /OrderSystem_war_exploded/customer_Add.jsp";
    }

    /**
     * 用户注册
     * @param
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public Map<String,Object> register(String username,String password,String password2){
        Map<String,Object> result = new HashMap<>();
        Boolean b = true;
        /*比对数据库账号*/
        List<String> usernameList = customerService.queryAllUsername();
        for (String userN:usernameList
        ) {
            if (userN.equals(username)){
                b=false;
            }
        }
        System.out.println(b);
        if(!(username==""&&password==""&&password2=="")){
            if (b){
                if (password.equals(password2)){

                    Customer customer = new Customer();
                    customer.setC_username(username);
                    customer.setC_password(password);
                    int i = customerService.insertOne(customer);
                    if (i>0){
                        result.put("code",1);
                        result.put("message","注册成功，请登陆");
                    }else {
                        result.put("code",2);
                        result.put("message","注册失败，请重新注册");
                    }
                }else {
                    result.put("code",0);
                    result.put("message","两次密码不相同");
                }

            }else {
                result.put("code",-1);
                result.put("message","账号已经存在");
            }
        }else {
            result.put("code",-2);
            result.put("message","账号或密码不能为空");
        }


        return result;
    }

    /**
     * 跳转到顾客页面
     * @return
     */
    @RequestMapping("/home")
    public String customerHome(){
        return "customer_Home";
    }
    /**
     * 跳转到主食页面
     * @return
     */
    @RequestMapping("/foodHome")
    public String foodHome(){
        return "menus/food";
    }

    /**
     * 顾客分类查询
     * @param pageNumber
     * @param pageSize
     * @param modelMap
     * @return
     */
    @RequestMapping("/foodAllClass")
    @ResponseBody
    public String queryAllMenus(@RequestParam("mClass") String mClass, Integer pageNumber, Integer pageSize, ModelMap modelMap ) {
        System.out.println(mClass);
        //处理参数
        int number;
        int size;
        if (pageNumber == null) {
            number = 1; //默认显示第1页
        } else {
            number = pageNumber;
        }
        if (pageSize == null) {
            size = 5; //默认每页显示五条
        } else {
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Menus> pageData = menusService.queryAllMenusByClass(mClass,number,size);
        System.out.println(pageData);
        //给视图传递上下文参数
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("pageDataC",pageData);
        //请求转发到success.jsp
        return "ok";
    }

    /**
     * 跳转到小吃页面
     * @return
     */
    @RequestMapping("/snackHome")
    public String snackHome(){
        return "menus/snack";
    }

    /**
     * 跳转到饮品页面
     * @return
     */
    @RequestMapping("/drinkHome")
    public String drinkHome(){
        return "menus/drink";
    }

    /**
     * 跳转到套餐页面
     * @return
     */
    @RequestMapping("/mealHome")
    public String mealHome(){
        return "menus/meal";
    }

    /**
     * 跳转到顾客个人信息页面
     * @return
     */
    @RequestMapping("/personalInfo")
    public String customerPersonalInfo(){
        return "personalInfo";
    }


    /**
     * 个人信息修改
     * @param customer
     * @return
     */
    @RequestMapping("/personalEdit")
    @ResponseBody
    public String personalInfo(@RequestBody Customer customer){
        String result;
        System.out.println(customer);
        int i = customerService.modifyCustomerInfo(customer);

        if (i>0){
            Customer customer1 = customerService.getOneById(customer.getC_id());
            Session session = SecurityUtils.getSubject().getSession();
            session.setAttribute("loginUser",customer1);
            result="ok";
            return result;
        }else {
            result="false";
            return result;
        }
    }


    @RequestMapping("/detailFood")
    public String queryById(int menusId, ModelMap modelMap){
        Menus menus = menusService.queryByMenusId(menusId);
        modelMap.put("menus",menus);
        return "menus/update";
    }

}
